package korkd.tower;


import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLoader;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TMXLoadException;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.util.Debug;

import android.content.Context;
import android.view.MotionEvent;
import android.widget.Toast;

public class Map implements IOnSceneTouchListener{
	// ===========================================================
	// Constants
	// ===========================================================

	// ===========================================================
	// Fields
	// ===========================================================

	//private BoundCamera mBoundChaseCamera;
	
	private TMXTiledMap mTMXTiledMap;
	private int mWidth;
	private int mHeight;
	private Road mRoad;
	private IUpdateHandler mGlideHandler;
	private float mTouchX;
	private float mTouchY;
	private float mTouchOffsetX;
	private float mTouchOffsetY;
	private Camera mCamera;
	
	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	
	public Map(TMXTiledMap pTMXTiledMap, Camera pCamera)
	{	
		mCamera = pCamera;
		mTMXTiledMap = pTMXTiledMap;
		setWidth(mTMXTiledMap.getTileColumns() * mTMXTiledMap.getTileWidth());
		setHeight(mTMXTiledMap.getTileRows() * mTMXTiledMap.getTileHeight());
		
		mRoad = new Road(mTMXTiledMap);
		mRoad.create();
		
		mGlideHandler = new IUpdateHandler()
		{
			@Override
			public void onUpdate(float pSecondsElapsed)
			{
				float newScrollX = mCamera.getCenterX() - mTouchOffsetX;
                float newScrollY = mCamera.getCenterY() - mTouchOffsetY;
               
                mCamera.setCenter(newScrollX, newScrollY);
                
                mTouchOffsetX *= 0.85f;
                mTouchOffsetY *= 0.85f;
                
                if(mTouchOffsetX == 0 && mTouchOffsetY == 0)
                {
                	
                }
			}

			@Override
			public void reset() {
				// TODO Auto-generated method stub
			}
		};
	}
	
	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pTouchEvent)
	{
		// Press Down
		if(pTouchEvent.getAction() == MotionEvent.ACTION_DOWN)
        {
				// Stop any gliding currently in progress
				pScene.unregisterUpdateHandler(mGlideHandler);
				
				// Get the current position of the touch event
                mTouchX = pTouchEvent.getMotionEvent().getX();
                mTouchY = pTouchEvent.getMotionEvent().getY();
        }
		// Move on the screen
        else if(pTouchEvent.getAction() == MotionEvent.ACTION_MOVE)
        {
        		// get the new position of the touch event
                float newX = pTouchEvent.getMotionEvent().getX();
                float newY = pTouchEvent.getMotionEvent().getY();
               
                // Determine the distance traveled from the previous touch
                mTouchOffsetX = (newX - mTouchX);
                mTouchOffsetY = (newY - mTouchY);
               
                // Determine the offset for the camera to shift
                float newScrollX = mCamera.getCenterX() - mTouchOffsetX;
                float newScrollY = mCamera.getCenterY() - mTouchOffsetY;
               
                // Set the new camera position
                mCamera.setCenter(newScrollX, newScrollY);
               
                // Update the variables
                mTouchX = newX;
                mTouchY = newY;
        }
		// On releasing touch event
        else if(pTouchEvent.getAction() == MotionEvent.ACTION_UP)
        {
        	// Start gliding
        	pScene.registerUpdateHandler(mGlideHandler);
        }
        return true;
	}
	
	// ===========================================================
	// Methods
	// ===========================================================

	public TMXTiledMap getMap() {
		return mTMXTiledMap;
	}

	public void setMap(TMXTiledMap mTMXTiledMap) {
		this.mTMXTiledMap = mTMXTiledMap;
	}

	public void setWidth(int mWidth) {
		this.mWidth = mWidth;
	}

	public int getWidth() {
		return mWidth;
	}

	public void setHeight(int mHeight) {
		this.mHeight = mHeight;
	}

	public int getHeight() {
		return mHeight;
	}
	
	public void createRoad(Castle c)
	{
		mRoad.create(c);
	}
	
	public void drawRoad()
	{
		mRoad.draw();
	}
	
	public Road getRoad()
	{
		return mRoad;
	}

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}